home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c
- Subject: Re: Reclaiming memory allocated recursively
- Date: 1 Jan 1996 13:20:00 GMT
- Organization: Kalevi, Inc
- Message-ID: <4c8n20$6k2@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe5.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Jan 02, 1996 02:52:35 in article <Reclaiming memory allocated
- recursively>, 'bvarley@yarrow.wt.com.au (bruce varley)' wrote:
-
- The code presented seems OK in concept; however, there's
- some places that look suspicious without seeing all of the code.
-
- >I'm using self-referential structures as described in K&R 6.5 (page
- >130 in my version). The program runs 'forever', storing data in a
- >binary tree - a days worh of data is collected, then dumped, and the
- >whole process starts all over again. My question is: How can I
- >reclaim the memory that has been recursively allocated? Using the
- >same approach as creating the tree - ie..........
- >
- > struct node *freetree (struct node *p)
- Just a comment: Functionally, freetree might as well be of type void
- since the return value is always 0; i.e., meaningless.
- > {
- > if (p != NULL)
- Does your code guarantee that p (i.e., left and right nodes) are NULL
- at the leaves? If not, a crash could result.
- > {
- > freetree (p -> left) ;
- > free (p -> data_pointer) ;
- Is data_pointer a unique pointer? In other words, could some other
- pointer possibly also point to the same data causing a memory block
- to be freed multiply?
- > freetree (p -> right) ;
- > }
- > return (0) ;
- > }
- >
- >seems unlikely to work, because the memory required to navigate
-
- Why unlikely? Makes sense to me -- or am I missing something?
-
- >through the tree is being deallocated as the process proceeds. In
- >fact, the system crashes when I run the routine.
-
- On another note, your code does not seem to release the memory for
- the tree nodes themselves. If you run this a lot, then you could crash
- due to running out of memory. Try sticking a "free(p);" after
- freeing the left and right nodes.
-
- --
-
- Pete
-
-
-
-
-
-